<template>
	<el-dialog
		title="选择查询字段"
		:visible.sync="propertyListShow"
		width="50%"
		:before-close="cancel"
		:close-on-click-modal="false"
		:close-on-press-escape="false"
	>
		<el-form ref="form" :model="form" label-width="120px" label-position="left" :rules="rules">
			<el-form-item label="基金类型" prop="fundRaiseType">
				<el-radio-group v-model="form.fundRaiseType" @change="radioChange">
					<el-radio label="1">公募基金</el-radio>
					<el-radio label="2">专户产品</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="产品运作状态">
				<el-select v-model="form.statusList" placeholder="请选择" size="medium" clearable filterable style="width: 100%" multiple>
					<el-option v-for="(item, index) in fundOpStatusList" :key="item.typeCode + index" :label="item.typeName" :value="item.typeCode">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="责任人：">
				<el-select
					:disabled="!form.fundRaiseType"
					v-model="form.personLiableId"
					placeholder="请选择"
					style="width: 100%"
					clearable
					filterable
				>
					<el-option
						v-for="(item, index) in personLiableList"
						:key="index + 'a'"
						:label="item.personLiableName"
						:value="item.personLiableId"
					/>
				</el-select>
			</el-form-item>
			<el-form-item label="复核人：">
				<el-select
					:disabled="!form.fundRaiseType"
					v-model="form.checkLiableId"
					placeholder="请选择"
					style="width: 100%"
					clearable
					filterable
				>
					<el-option
						v-for="(item, index) in checkLiableList"
						:key="index + 'a'"
						:label="item.checkLiableName"
						:value="item.checkLiableId"
					/>
				</el-select>
			</el-form-item>
			<el-form-item label="选择字段">
				<el-cascader
					:disabled="!!!form.fundRaiseType"
					style="width: 100%"
					:options="propertyData"
					:props="{ multiple: true }"
					filterable
					clearable
					@change="cascChange"
					v-model="propertyList"
					:key="cascKey"
				>
				</el-cascader>
			</el-form-item>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="cancel" size="small">取 消</el-button>
			<el-button type="primary" @click="addConfirm" size="small">确 定</el-button>
		</span>
	</el-dialog>
</template>
<script>
import { fundTypePublic, propertyString } from '../../propertyData'
import { getEnum, jsonArray } from '@/api/productApi/file'
export default {
	name: 'PropertyList',

	props: {
		propertyListShow: {
			type: Boolean,
			default: false
		},
		managerList: {
			type: Array,
			default: Array
		},
		checckPersonList: {
			type: Array,
			default: Array
		},
		propertyListFunc: {
			type: Function,
			default: Function
		}
	},

	data() {
		return {
			propertyList: [['baseInfo', 'fundCode'], ['baseInfo', 'fundFullName'], ['status']],
			propertyData: [],
			form: {
				fundRaiseType: '',
				personLiableId: '',
				statusList: [],
				checkLiableId: '',
				graphQlParams: 'baseInfo{fundFullName,fundCode},status'
			},
			fundOpStatusList: [],
			personLiableList: [],
			checkLiableList: [],
			cascKey: 1,
			rules: {
				fundRaiseType: [{ required: true, message: '请选择基金类型', trigger: 'change' }]
			}
		}
	},

	mounted() {
		this.getEnumData()
	},

	methods: {
		cancel() {
			this.$emit('propertyListFunc')
			this.propertyList = [['baseInfo', 'fundCode'], ['baseInfo', 'fundFullName'], ['status']]
			this.form.fundRaiseType = ''
			this.form.personLiableId = ''
			this.form.checkLiableId = ''
			this.form.statusList = []
			this.propertyData = []
			this.form.graphQlParams = 'baseInfo{fundFullName,fundCode},status'
			this.cascKey += 1
			this.$refs.form.resetFields()
		},

		cascChange() {
			[['baseInfo', 'fundCode'], ['baseInfo', 'fundFullName'], ['status']].forEach(item => {
				if (!this.propertyList.includes(item)) {
					this.propertyList.push(item)
				}
			})
			this.form.graphQlParams = propertyString(jsonArray(this.propertyList))
		},

		getEnumData() {
			getEnum(['fundOperationStatus']).then(res => {
				if (res.msgCode === 'Y') {
					this.fundOpStatusList = res.data.body.fundOperationStatus
				}
			})
		},

		// 新增或编辑
		addConfirm() {
			this.$refs.form.validate(valid => {
				if (valid) {
					this.$emit('confrim', {
						graphQlParams: this.form.graphQlParams,
						fundRaiseType: this.form.fundRaiseType,
						statusList: this.form.statusList,
						personLiableId: this.form.personLiableId,
						checkLiableId: this.form.checkLiableId
					})
				}
			})
		},

		edit(obj) {},

		radioChange() {
			this.form.personLiableId = ''
			this.form.checkLiableId = ''
			this.personLiableList = this.managerList.filter(item => item.fundRaiseType === this.form.fundRaiseType)
			this.checkLiableList = this.checckPersonList.filter(item => item.fundRaiseType === this.form.fundRaiseType)
			this.propertyList = [['baseInfo', 'fundCode'], ['baseInfo', 'fundFullName'], ['status']]
			let prorertyList = jsonArray(fundTypePublic(this.form.fundRaiseType))
			if (this.form.fundRaiseType === '1') {
				// 如是公募基金，则去掉委托人信息、风控信息
				prorertyList = prorertyList.filter(item => item.label !== '委托人信息' && item.label !== '风控信息')
				const dateProperty = ['initialFilingDate', 'finishFilingDate'] // 要去掉的专户日期信息字段
				const organInfoProperty = ['investAdvisorOrgan', 'investAdvisorRate'] // 要去掉的专户机构信息字段
				prorertyList.forEach(item => {
					if (item.value === 'dateInfo') {
						item.children = item.children.filter(items => !dateProperty.includes(items.value))
						item.children.forEach(items => {
							if (items.value === 'establishDate') {
								items.label = '合同生效日'
							}
						})
					}
					if (item.value === 'organInfo') {
						item.children = item.children.filter(items => !organInfoProperty.includes(items.value))
					}
					if (item.value === 'baseInfo') {
						item.children.forEach(items => {
							if (items.value === 'fundCode') {
								items.label = '基金代码（六位）'
							}
						})
					}
				})
			}
			if (this.form.fundRaiseType === '2') {
				// 如是专户基金，则去掉下属基金
				const dateProperty = ['endListDate', 'approvalDate', 'logonApprovalChangeDate', 'deferRaiseDate', 'firstRedemptionDate'] // 要去掉的专户日期信息字段
				const inverProperty = ['isQuantTactics', 'isCoverTactics'] // 要去掉的投资相关信息字段
				prorertyList.forEach(item => {
					if (item.label === '基础信息') {
						if (item.children && Array.isArray(item.children) && item.children.length) {
							item.children = item.children.filter(item => item.label !== '下属列表')
						}
					}
					if (item.label === '基金经理') {
						item.label = '投资经理'
					}
					if (item.value === 'dateInfo') {
						item.children = item.children.filter(items => !dateProperty.includes(items.value))
					}
					if (item.value === 'investInfo') {
						item.children = item.children.filter(items => !inverProperty.includes(items.value))
					}
				})
			}
			this.propertyData = prorertyList
		}
	}
}
</script>
<style lang="scss" scoped ></style>
